import processing.core.PApplet;
import processing.core.PImage;

/**
 * This class's only purpose is to calculate average color of the <code>PImage</code> that 
 * is given to the calculator's <code>calculateColor</code> method.
 * 
 * @author anastasia
 *
 */
public class ColorCalculator {
	/** The parent applet that id needed for getting and creating color values.*/
	public PApplet parent;
	
	public ColorCalculator(PApplet p) {
		this.parent = p;
	}
	
	/**
	 * Calculates the average color of <code>image</code> using every pixel's RGB-color value.
	 * 
	 * @param image	to be represented by one color
	 * @return	the average color for given <code>image</code>
	 */
	public int calculateColor(PImage image) {
		image.loadPixels();
		int pixels[] = image.pixels;
		
		int reds = 0;
		int greens = 0;
		int blues = 0;
		
		int clr;
		int i;
		for(i = 0; i < pixels.length; i++) {
			clr = pixels[i];
			reds += parent.red(clr);
			greens += parent.green(clr);
			blues += parent.blue(clr);
		}
		
		int average = parent.color(reds/i, greens/i, blues/i);
		
		return average;
	}

}
